package com.example.tractorframework.helloworld;

import android.view.MotionEvent;
import android.view.View;

import com.tractorframework.core.Event;
import com.tractorframework.core.receptor.IOperationListener;
import com.tractorframework.core.receptor.IViewReceptor;
import com.tractorframework.core.receptor.ViewReceptorBase;

public class ScrollReceptor extends ViewReceptorBase implements IViewReceptor {

	private ScrollOperation operation ;
	
	private View bt;
	
	@Override
	protected void onStartOverride(View view) {
		
		operation = new ScrollOperation();
		
		view.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View view, MotionEvent event) {
				onViewTouch(view, event);
				return true;
			}
		});
		operation.addOperationListener(Events.Scroll, new IOperationListener() {


			@Override
			public void onOperation(Object target, Event event) {
				onScroll((Float)event.getData());
			}
		});
	}
	
	private void onViewTouch(View view, MotionEvent event)
	{
		switch(event.getAction())
		{
			case MotionEvent.ACTION_DOWN:
				beginOperate(operation, view, event);
				break;
			default:
				operate(operation, view, event);
		}
	}
	
	private void onScroll(float delta)
	{
		bt.setTranslationX(bt.getTranslationX() + delta);
	}

	@Override
	protected void onStopOverride(View view) {
		
	}

}
